##################### Script Routes to Representation ################
### Done by Andrea Vik, if there are any questions please email me at andrea.vik@rhul.ac.uk

# R version 4.1.1 (2021-08-10)
# Platform: x86_64-apple-darwin17.0 (64-bit)
# Running under: macOS Monterey 12.4

# attached base packages:
# [1] tools     stats     graphics  grDevices utils     datasets  methods   base     

# other attached packages:
#  [1] sandwich_3.0-2        lmtest_0.9-39         zoo_1.8-9             devtools_2.4.2        usethis_2.1.3         dotwhisker_0.7.4      sjlabelled_1.2.0      sjPlot_2.8.11        
#  [9] kableExtra_1.3.4      estimatr_1.0.0        ggpubr_0.4.0          modelsummary_0.11.1   ggthemr_1.1.0         cjpowR_1.0.0          cregg_0.4.0           forcats_0.5.1        
# [17] stringr_1.4.0         dplyr_1.0.7           purrr_0.3.4           tidyr_1.1.4           tibble_3.1.6          ggplot2_3.4.2         tidyverse_1.3.1       sjmisc_2.8.9         
# [25] janitor_2.2.0         GPArotation_2014.11-1 psych_2.1.9           readr_2.1.2          

##############Preprocessing and descriptives ############

#######PACKAGES#########
if (!require(readr)) {install.packages("readr"); library(readr)}
if (!require(psych)) {install.packages("psych"); library(psych)}
if (!require(GPArotation)) {install.packages("GPArotation"); library(GPArotation)}
if (!require(janitor)) {install.packages("janitor"); library(janitor)}
if (!require(sjmisc)) {install.packages("sjmisc"); library(sjmisc)}
if (!require(tidyverse)) {install.packages("tidyverse"); library(tidyverse)}
if (!require(tools)) {install.packages("tools"); library(tools)}
if (!require(cregg)) {install.packages("cregg"); library(cregg)}
if (!require(cjpowR)) {install.packages("cjpowR"); library(cjpowR)}
if (!require(dplyr)) {install.packages("dplyr"); library(dplyr)}
if (!require(modelsummary)) {install.packages("dplyr"); library(modelsummary)}
if (!require(ggpubr)) {install.packages("ggpubr"); library(ggpubr)}
if (!require(ggplot2)) {install.packages("ggplot2"); library(ggplot2)}
if (!require(devtools)) {install.packages("devtools"); library(devtools)}
if (!require(kableExtra)) {install.packages("kableExtra"); library(kableExtra)}

#setting theme
##setting a theme
devtools::install_github('cttobin/ggthemr')
library(ggthemr)
ggthemr("dust", layout="scientific", text_size = 16, set_theme = TRUE) #setting theme for all figures 

#############################POWER ANALYSIS ####################################
cjpowr_amce(amce = 0.02, n = 256000, levels = 6, alpha=0.003) #n=8000
#power of 99%
cjpowr_amce(amce = 0.02, n = 128000, levels = 6, alpha=0.003) #n=2000
#power of 87%
#this function only works for R version 4.1.1, but this is how the power was determined.

########################## PREPOROCESSING ######################################
## Importing data ##
widedata <- read_csv("wideconjointdata.csv") #change to your working directory
widedata <- wideconjointdata

### PREPROCESSING: Creating scales ########
##########PERSONALITY
my.keys <- make.keys(widedata, list(agree = c("Q1_1_1","-Q1_1_2","-Q1_1_3", "Q1_1_4"),
                                    open= c("-Q1_1_5","Q1_1_6","-Q1_1_7", "-Q1_1_8"),
                                    consc = c("-Q1_1_9","Q1_1_10","-Q1_1_11", "Q1_1_12"),
                                    narcs = c("Q1_2_1","Q1_2_2","Q1_2_3", "Q1_2_4")))

my.scales <- scoreItems(my.keys, widedata)
my.scales #show the output
#aplpha: (Unstandardized) Alpha:
#agree open consc narcs
#alpha  0.69 0.61  0.63  0.82
print(my.scales,short=FALSE)
# saving the scales in the data:
widedata <- cbind(widedata, my.scales$scores)

################## Political Efficacy
pe.keys <- make.keys(widedata, list(politicalefficacy = c("Q4_1_1","Q4_1_2","Q4_1_3", "Q4_1_4")))
pe.scales <- scoreItems(pe.keys, widedata)
pe.scales #show the output
#aplpha: 0.66
print(pe.scales,short=FALSE)
# saving the scales in the data:
widedata <- cbind(widedata, pe.scales$scores)
#external political efficacy
expe.keys <- make.keys(widedata, list(expoliticalefficacy = c("Q4_1_2","Q4_1_4")))
expe.scales <- scoreItems(expe.keys, widedata)
expe.scales #show the output
#aplpha: 0.79
print(expe.scales,short=FALSE)
# saving the scales in the data:
widedata <- cbind(widedata, expe.scales$scores)
#internalpoliticaleff
inpe.keys <- make.keys(widedata, list(intpoliticalefficacy = c("Q4_1_1","Q4_1_3")))
inpe.scales <- scoreItems(inpe.keys, widedata)
inpe.scales #show the output
#aplpha: 0.78
print(inpe.scales,short=FALSE)
# saving the scales in the data:
widedata <- cbind(widedata, inpe.scales$scores)


####Media Use
widedata$socialmedia <- (widedata$Q4_5_4 + widedata$Q4_5_5 + widedata$Q4_5_6) / 3 #meanscore socialmedia
widedata$traditionalmedia <- (widedata$Q4_5_1 + widedata$Q4_5_2 + widedata$Q4_5_3) / 3 #meanscore traditionalmedia

##renaming variables to make them meaningful
widedata <- rename(widedata, taxposition = Q2_2_Q2_2a, immigrationposition = Q2_2_Q2_2b, climateposition = Q2_2_Q2_2c, socialtrust = Q3_1, radio = Q4_5_1, newspapers = Q4_5_2, TV = Q4_5_3, facebook = Q4_5_4, youtube = Q4_5_5, twitter =Q4_5_6, politicalideology = Q2_1, politicalinterest = Q2_3, socialtrust = Q3_1)

##############################################################SAMPLE DESCRIPTIVES##################################################
##total sample
totalnetdata <- widedata %>% dplyr::select("country", "age", "gender", "education", "socialclass", "politicalideology", "politicalinterest", "socialtrust", "socialmedia", "traditionalmedia", "agree", "open", "consc", "narcs", "climateposition", "immigrationposition", "taxposition")
totalnetdata <- rename(totalnetdata, agreeableness = agree, openness = open, conscientiousness = consc, narcissism = narcs)
totalnetdata$country <- factor(totalnetdata$country)
totalnetdata$country <- recode_factor(totalnetdata$country, "1" = "DE", "2" = "IT", "3" = "RO", "4" = "SW")

##latex table
datasummary_balance(~country, totalnetdata, output = "latex", title = "Descriptives by Country") #tableforlatex
##html table
datasummary_skim(totalnetdata, output = "html")

#for further analysis we need to compute values for NAs. all variables to be included in the model has less than 10% missing, so we insert the median instead of mean as its better for calculating congruences.
widedata <- widedata %>% 
  dplyr::mutate_at(vars(politicalinterest, taxposition, immigrationposition, climateposition, socialmedia, politicalideology, politicalefficacy, intpoliticalefficacy, expoliticalefficacy), ~replace_na(.,median(., na.rm = TRUE)))


################################ DATA WRANGLING: wide to long #################################
wideconjoint1 <- widedata %>%
  select(Respondent_Serial, country, gender, age, immigrationposition, climateposition, taxposition, narcs, agree, open, consc, politicalinterest, socialmedia, socialtrust, socialclass, politicalideology, intpoliticalefficacy, expoliticalefficacy, politicalefficacy, 57:103) #make sure it is all variables between version and duration
#widetolongformat 
library(cregg)
#### transforming wide to long
list3 <- list(
  Cgender = list(
    ~ a1_t1_c1 + a1_t2_c1 + a1_t3_c1 + a1_t4_c1,
    ~ a1_t1_c2 + a1_t2_c2 + a1_t3_c2 + a1_t4_c2
  ),
  Cage = list(
    ~ a2_t1_c1 + a2_t2_c1 + a2_t3_c1 + a2_t4_c1,
    ~ a2_t1_c2 + a2_t2_c2 + a2_t3_c2 + a2_t4_c2
  ),
  Cpersonality = list(
    ~ a3_t1_c1 + a3_t2_c1 + a3_t3_c1 + a3_t4_c1,
    ~ a3_t1_c2 + a3_t2_c2 + a3_t3_c2 + a3_t4_c2
  ),
  Cissueposition = list(
    ~ a4_t1_c1 + a4_t2_c1 + a4_t3_c1 + a4_t4_c1,
    ~ a4_t1_c2 + a4_t2_c2 + a4_t3_c2 + a4_t4_c2
  ),
  Crating = list(
    ~ FFTRandom1_rankA + FFTRandom2_rankA + FFTRandom3_rankA + FFTRandom4_rankA,
    ~ FFTRandom1_rankB + FFTRandom2_rankB + FFTRandom3_rankB + FFTRandom4_rankB
  )
)
# task variables
list2 <- list(choice = ~ FFTRandom1_response + FFTRandom2_response + FFTRandom3_response + FFTRandom4_response)

## perform reshape
str(long <- cj_tidy(wideconjoint1,
                    profile_variables = list3,
                    task_variables = list2,
                    id = ~ Respondent_Serial)) + stopifnot(nrow(long) == nrow(wideconjoint1)*4*2)
#making the chosen DV meaningful:
long$chosen <- ifelse((long$profile == "A" & long$choice == 1) |
                        (long$profile == "B" & long$choice == 2), 1, 0)

############################### DATA TRANSFORMATION: Coding congruences #######################################
##gender
longconjoint <- long %>%  dplyr::mutate(CONG_GENDER = case_when(gender == Cgender ~ 1, TRUE ~ 0))
longconjoint %>% count(CONG_GENDER)

#age
longconjoint <- longconjoint %>%  mutate(
  CONG_AGE = case_when(
    (age == 20 & Cage == 1) | (age == 35 & Cage == 2) | (age == 55 & Cage == 3) | (age == 70 & Cage == 4) ~ 70,
    (age == 21 & Cage == 1) | (age == 19 & Cage == 1) | (age == 36 & Cage == 2) | (age == 34 & Cage == 2) | (age == 56 & Cage == 3) | (age == 54 & Cage == 3) | (age == 71 & Cage == 4) | (age == 69 & Cage == 4) ~ 69,
    (age == 22 & Cage == 1) | (age == 18 & Cage == 1) | (age == 37 & Cage == 2) | (age == 33 & Cage == 2) | (age == 57 & Cage == 3) | (age == 53 & Cage == 3) | (age == 72 & Cage == 4) | (age == 68 & Cage == 4) ~ 68,
    (age == 23 & Cage == 1) | (age == 38 & Cage == 2) | (age == 32 & Cage == 2) | (age == 58 & Cage == 3) | (age == 52 & Cage == 3) | (age == 73 & Cage == 4) | (age == 67 & Cage == 4) ~ 67,
    (age == 24 & Cage == 1) | (age == 39 & Cage == 2) | (age == 31 & Cage == 2) | (age == 59 & Cage == 3) | (age == 51 & Cage == 3) | (age == 74 & Cage == 4) | (age == 66 & Cage == 4) ~ 66,
    (age == 25 & Cage == 1) | (age == 40 & Cage == 2) | (age == 30 & Cage == 2) | (age == 60 & Cage == 3) | (age == 50 & Cage == 3) | (age == 75 & Cage == 4) | (age == 65 & Cage == 4) ~ 65,
    (age == 26 & Cage == 1) | (age == 41 & Cage == 2) | (age == 29 & Cage == 2) | (age == 61 & Cage == 3) | (age == 49 & Cage == 3) | (age == 76 & Cage == 4) | (age == 64 & Cage == 4) ~ 64,
    (age == 27 & Cage == 1) | (age == 42 & Cage == 2) | (age == 28 & Cage == 2) | (age == 62 & Cage == 3) | (age == 48 & Cage == 3) | (age == 77 & Cage == 4) | (age == 63 & Cage == 4) ~ 63,
    (age == 28 & Cage == 1) | (age == 43 & Cage == 2) | (age == 27 & Cage == 2) | (age == 63 & Cage == 3) | (age == 47 & Cage == 3) | (age == 78 & Cage == 4) | (age == 62 & Cage == 4) ~ 62,
    (age == 29 & Cage == 1) | (age == 44 & Cage == 2) | (age == 26 & Cage == 2) | (age == 64 & Cage == 3) | (age == 46 & Cage == 3) | (age == 79 & Cage == 4) | (age == 61 & Cage == 4) ~ 61,
    (age == 30 & Cage == 1) | (age == 45 & Cage == 2) | (age == 25 & Cage == 2) | (age == 65 & Cage == 3) | (age == 45 & Cage == 3) | (age == 80 & Cage == 4) | (age == 60 & Cage == 4) ~ 60,
    (age == 31 & Cage == 1) | (age == 46 & Cage == 2) | (age == 24 & Cage == 2) | (age == 66 & Cage == 3) | (age == 44 & Cage == 3) | (age == 81 & Cage == 4) | (age == 59 & Cage == 4) ~ 59,
    (age == 32 & Cage == 1) | (age == 47 & Cage == 2) | (age == 23 & Cage == 2) | (age == 67 & Cage == 3) | (age == 43 & Cage == 3) | (age == 82 & Cage == 4) | (age == 58 & Cage == 4) ~ 58,
    (age == 33 & Cage == 1) | (age == 48 & Cage == 2) | (age == 22 & Cage == 2) | (age == 68 & Cage == 3) | (age == 42 & Cage == 3) | (age == 83 & Cage == 4) | (age == 57 & Cage == 4) ~ 57,
    (age == 34 & Cage == 1) | (age == 49 & Cage == 2) | (age == 21 & Cage == 2) | (age == 69 & Cage == 3) | (age == 41 & Cage == 3) | (age == 84 & Cage == 4) | (age == 56 & Cage == 4) ~ 56,
    (age == 35 & Cage == 1) | (age == 50 & Cage == 2) | (age == 20 & Cage == 2) | (age == 70 & Cage == 3) | (age == 40 & Cage == 3) | (age == 85 & Cage == 4) | (age == 55 & Cage == 4) ~ 55,
    (age == 36 & Cage == 1) | (age == 51 & Cage == 2) | (age == 19 & Cage == 2) | (age == 71 & Cage == 3) | (age == 39 & Cage == 3) | (age == 86 & Cage == 4) | (age == 54 & Cage == 4) ~ 54,
    (age == 37 & Cage == 1) | (age == 52 & Cage == 2) | (age == 18 & Cage == 2) | (age == 72 & Cage == 3) | (age == 38 & Cage == 3) | (age == 87 & Cage == 4) | (age == 53 & Cage == 4) ~ 53,
    (age == 38 & Cage == 1) | (age == 53 & Cage == 2) | (age == 73 & Cage == 3) | (age == 37 & Cage == 3) | (age == 88 & Cage == 4) | (age == 52 & Cage == 4) ~ 52,
    (age == 39 & Cage == 1) | (age == 54 & Cage == 2) | (age == 74 & Cage == 3) | (age == 36 & Cage == 3) | (age == 89 & Cage == 4) | (age == 51 & Cage == 4) ~ 51,
    (age == 40 & Cage == 1) | (age == 55 & Cage == 2) | (age == 75 & Cage == 3) | (age == 35 & Cage == 3) | (age == 90 & Cage == 4) | (age == 50 & Cage == 4) ~ 50,
    (age == 41 & Cage == 1) | (age == 56 & Cage == 2) | (age == 76 & Cage == 3) | (age == 34 & Cage == 3) ~ 49,
    (age == 42 & Cage == 1) | (age == 57 & Cage == 2) | (age == 77 & Cage == 3) | (age == 33 & Cage == 3) ~ 48,
    (age == 43 & Cage == 1) | (age == 58 & Cage == 2) | (age == 78 & Cage == 3) | (age == 32 & Cage == 3) ~ 47,
    (age == 44 & Cage == 1) | (age == 59 & Cage == 2) | (age == 79 & Cage == 3) | (age == 31 & Cage == 3) ~ 46,
    (age == 45 & Cage == 1) | (age == 60 & Cage == 2) | (age == 80 & Cage == 3) | (age == 30 & Cage == 3) ~ 45,
    (age == 46 & Cage == 1) | (age == 61 & Cage == 2) | (age == 81 & Cage == 3) | (age == 29 & Cage == 3) ~ 44,
    (age == 47 & Cage == 1) | (age == 62 & Cage == 2) | (age == 82 & Cage == 3) | (age == 28 & Cage == 3) ~ 43,
    (age == 48 & Cage == 1) | (age == 63 & Cage == 2) | (age == 83 & Cage == 3) | (age == 27 & Cage == 3) ~ 42,
    (age == 49 & Cage == 1) | (age == 64 & Cage == 2) | (age == 84 & Cage == 3) | (age == 26 & Cage == 3) ~ 41,
    (age == 50 & Cage == 1) | (age == 65 & Cage == 2) | (age == 85 & Cage == 3) | (age == 25 & Cage == 3) ~ 40,
    (age == 51 & Cage == 1) | (age == 66 & Cage == 2) | (age == 86 & Cage == 3) | (age == 24 & Cage == 3) ~ 39,
    (age == 52 & Cage == 1) | (age == 67 & Cage == 2) | (age == 87 & Cage == 3) | (age == 23 & Cage == 3) ~ 38,
    (age == 53 & Cage == 1) | (age == 68 & Cage == 2) | (age == 88 & Cage == 3) | (age == 22 & Cage == 3) ~ 37,
    (age == 54 & Cage == 1) | (age == 69 & Cage == 2) | (age == 89 & Cage == 3) | (age == 21 & Cage == 3) ~ 36,
    (age == 55 & Cage == 1) | (age == 70 & Cage == 2) | (age == 90 & Cage == 3) | (age == 20 & Cage == 3) ~ 35,
    (age == 56 & Cage == 1) | (age == 71 & Cage == 2) | (age == 19 & Cage == 3) | (age == 49 & Cage == 4) ~ 34,
    (age == 57 & Cage == 1) | (age == 72 & Cage == 2) | (age == 18 & Cage == 3) | (age == 48 & Cage == 4) ~ 33,
    (age == 58 & Cage == 1) | (age == 73 & Cage == 2) | (age == 47 & Cage == 4) ~ 32,
    (age == 59 & Cage == 1) | (age == 74 & Cage == 2) | (age == 46 & Cage == 4) ~ 31,
    (age == 60 & Cage == 1) | (age == 75 & Cage == 2) | (age == 45 & Cage == 4) ~ 30,
    (age == 61 & Cage == 1) | (age == 76 & Cage == 2) | (age == 44 & Cage == 4) ~ 29,
    (age == 62 & Cage == 1) | (age == 77 & Cage == 2) | (age == 43 & Cage == 4) ~ 28,
    (age == 63 & Cage == 1) | (age == 78 & Cage == 2) | (age == 42 & Cage == 4) ~ 27,
    (age == 64 & Cage == 1) | (age == 79 & Cage == 2) | (age == 41 & Cage == 4) ~ 26,
    (age == 65 & Cage == 1) | (age == 80 & Cage == 2) | (age == 40 & Cage == 4) ~ 25,
    (age == 66 & Cage == 1) | (age == 81 & Cage == 2) | (age == 39 & Cage == 4) ~ 24,
    (age == 67 & Cage == 1) | (age == 82 & Cage == 2) | (age == 38 & Cage == 4) ~ 23,
    (age == 68 & Cage == 1) | (age == 83 & Cage == 2) | (age == 37 & Cage == 4) ~ 22,
    (age == 69 & Cage == 1) | (age == 84 & Cage == 2) | (age == 36 & Cage == 4) ~ 21,
    (age == 70 & Cage == 1) | (age == 85 & Cage == 2) | (age == 35 & Cage == 4) ~ 20,
    (age == 71 & Cage == 1) | (age == 86 & Cage == 2) | (age == 34 & Cage == 4) ~ 19,
    (age == 72 & Cage == 1) | (age == 87 & Cage == 2) | (age == 33 & Cage == 4) ~ 18,
    (age == 73 & Cage == 1) | (age == 88 & Cage == 2) | (age == 32 & Cage == 4) ~ 17,
    (age == 74 & Cage == 1) | (age == 89 & Cage == 2) | (age == 31 & Cage == 4) ~ 16,
    (age == 75 & Cage == 1) | (age == 90 & Cage == 2) | (age == 30 & Cage == 4) ~ 15,
    (age == 76 & Cage == 1) | (age == 29 & Cage == 4) ~ 14,
    (age == 77 & Cage == 1) | (age == 28 & Cage == 4)~ 13,
    (age == 78 & Cage == 1) | (age == 27 & Cage == 4)~ 12,
    (age == 79 & Cage == 1) | (age == 26 & Cage == 4)~ 11,
    (age == 80 & Cage == 1) | (age == 25 & Cage == 4)~ 10,
    (age == 81 & Cage == 1) | (age == 24 & Cage == 4)~ 9,
    (age == 82 & Cage == 1) | (age == 23 & Cage == 4)~ 8,
    (age == 83 & Cage == 1) | (age == 22 & Cage == 4)~ 7,
    (age == 84 & Cage == 1) | (age == 21 & Cage == 4)~ 6,
    (age == 85 & Cage == 1) | (age == 20 & Cage == 4)~ 5,
    (age == 86 & Cage == 1) | (age == 19 & Cage == 4)~ 4,
    (age == 87 & Cage == 1) | (age == 18 & Cage == 4)~ 3,
    (age == 88 & Cage == 1) ~ 2,
    (age == 89 & Cage == 1) ~ 1,
    (age == 90 & Cage == 1) ~ 0))
longconjoint %>% count(CONG_AGE)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_AGE_B = case_when(
    (between(age, 18, 26) & Cage == 1) | (between(age, 26, 41) & Cage == 2) | (between(age, 41, 60) & Cage == 3) | (between(age, 60, 90) & Cage == 4) ~ 1, 
    TRUE ~ 0))
longconjoint %>% count(CONG_AGE_B)

#standardized
longconjoint$std_CONG_AGE <- longconjoint$CONG_AGE / 70
longconjoint %>% count(std_CONG_AGE)

###Issue congruence###
#Cissueposition 1,3,5 is left issueposition 1=pro and 10=anti
#climate
#continous
longconjoint <- longconjoint %>%  mutate(
  CONG_CLI = case_when(
    (between(climateposition, 9, 10) & Cissueposition == 1) | (between(climateposition, 1, 2) & Cissueposition == 2) ~ -4,
    (between(climateposition, 8, 9) & Cissueposition == 1) | (between(climateposition, 2, 3) & Cissueposition == 2)~ -3,
    (between(climateposition, 7, 8) & Cissueposition == 1) | (between(climateposition, 3, 4) & Cissueposition == 2) ~ -2,
    (between(climateposition, 6, 7) & Cissueposition == 1) | (between(climateposition, 4, 5) & Cissueposition == 2)~ -1,
    (between(climateposition, 5, 6) & Cissueposition == 1) | (between(climateposition, 5, 6) & Cissueposition == 2)~ 0,
    (between(climateposition, 4, 5) & Cissueposition == 1) | (between(climateposition, 6, 7) & Cissueposition == 2)~ 1,
    (between(climateposition, 3, 4) & Cissueposition == 1) | (between(climateposition, 7, 8) & Cissueposition == 2)~ 2,
    (between(climateposition, 2, 3) & Cissueposition == 1) | (between(climateposition, 8, 9) & Cissueposition == 2)~ 3,
    (between(climateposition, 1, 2) & Cissueposition == 1) | (between(climateposition, 9, 10) & Cissueposition == 2)~ 4,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_CLI)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_CLI_B = case_when(
    (between(climateposition, 5.5, 10) & Cissueposition == 1) | (between(climateposition, 1, 5.5) & Cissueposition == 2) ~ 0,
    (between(climateposition, 1, 5.5) & Cissueposition == 1) | (between(climateposition, 5.5, 10) & Cissueposition == 2)~ 1,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_CLI_B)

#standardized
longconjoint$std_CONG_CLI <- (longconjoint$CONG_CLI + 4) / 8
longconjoint %>% count(std_CONG_CLI)

###immigration
longconjoint <- longconjoint %>%  mutate(
  CONG_IMM = case_when(
    (between(immigrationposition, 9, 10) & Cissueposition == 3) | (between(immigrationposition, 1, 2) & Cissueposition == 4) ~ -4,
    (between(immigrationposition, 8, 9) & Cissueposition == 3) | (between(immigrationposition, 2, 3) & Cissueposition == 4)~ -3,
    (between(immigrationposition, 7, 8) & Cissueposition == 3) | (between(immigrationposition, 3, 4) & Cissueposition == 4) ~ -2,
    (between(immigrationposition, 6, 7) & Cissueposition == 3) | (between(immigrationposition, 4, 5) & Cissueposition == 4)~ -1,
    (between(immigrationposition, 5, 6) & Cissueposition == 3) | (between(immigrationposition, 5, 6) & Cissueposition == 4)~ 0,
    (between(immigrationposition, 4, 5) & Cissueposition == 3) | (between(immigrationposition, 6, 7) & Cissueposition == 4)~ 1,
    (between(immigrationposition, 3, 4) & Cissueposition == 3) | (between(immigrationposition, 7, 8) & Cissueposition == 4)~ 2,
    (between(immigrationposition, 2, 3) & Cissueposition == 3) | (between(immigrationposition, 8, 9) & Cissueposition == 4)~ 3,
    (between(immigrationposition, 1, 2) & Cissueposition == 3) | (between(immigrationposition, 9, 10) & Cissueposition == 4)~ 4,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_IMM)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_IMM_B = case_when(
    (between(immigrationposition, 5.5, 10) & Cissueposition == 3) | (between(immigrationposition, 1, 5.5) & Cissueposition == 4) ~ 0,
    (between(immigrationposition, 1, 5.5) & Cissueposition == 3) | (between(immigrationposition, 5.5, 10) & Cissueposition == 4)~ 1,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_IMM_B)

#standardized
longconjoint$std_CONG_IMM <- (longconjoint$CONG_IMM + 4) / 8
longconjoint %>% count(std_CONG_IMM)

##taxation
longconjoint <- longconjoint %>%  mutate(
  CONG_TAX = case_when(
    (between(taxposition, 9, 10) & Cissueposition == 5) | (between(taxposition, 1, 2) & Cissueposition == 6) ~ -4,
    (between(taxposition, 8, 9) & Cissueposition == 5) | (between(taxposition, 2, 3) & Cissueposition == 6)~ -3,
    (between(taxposition, 7, 8) & Cissueposition == 5) | (between(taxposition, 3, 4) & Cissueposition == 6) ~ -2,
    (between(taxposition, 6, 7) & Cissueposition == 5) | (between(taxposition, 4, 5) & Cissueposition == 6)~ -1,
    (between(taxposition, 5, 6) & Cissueposition == 5) | (between(taxposition, 5, 6) & Cissueposition == 6)~ 0,
    (between(taxposition, 4, 5) & Cissueposition == 5) | (between(taxposition, 6, 7) & Cissueposition == 6)~ 1,
    (between(taxposition, 3, 4) & Cissueposition == 5) | (between(taxposition, 7, 8) & Cissueposition == 6)~ 2,
    (between(taxposition, 2, 3) & Cissueposition == 5) | (between(taxposition, 8, 9) & Cissueposition == 6)~ 3,
    (between(taxposition, 1, 2) & Cissueposition == 5) | (between(taxposition, 9, 10) & Cissueposition == 6)~ 4,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_TAX)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_TAX_B = case_when(
    (between(taxposition, 5.5, 10) & Cissueposition == 5) | (between(taxposition, 1, 5.5) & Cissueposition == 6) ~ 0,
    (between(taxposition, 1, 5.5) & Cissueposition == 5) | (between(taxposition, 5.5, 10) & Cissueposition == 6)~ 1,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_TAX_B)

#standardized
longconjoint$std_CONG_TAX <- (longconjoint$CONG_TAX + 4) / 8
longconjoint %>% count(std_CONG_TAX)

##issuecongruence overall
longconjoint <- longconjoint %>%  mutate(
  CONG_ISSUE = case_when((CONG_CLI == -4) | (CONG_IMM == -4) | (CONG_TAX == -4) ~ -4,
                         (CONG_CLI == -3) | (CONG_IMM == -3) | (CONG_TAX == -3) ~ -3,
                         (CONG_CLI == -2) | (CONG_IMM == -2) | (CONG_TAX == -2) ~ -2,
                         (CONG_CLI == -1) | (CONG_IMM == -1) | (CONG_TAX == -1) ~ -1,
                         (CONG_CLI == 0) | (CONG_IMM == 0) | (CONG_TAX == 0) ~ 0,
                         (CONG_CLI == 1) | (CONG_IMM == 1) | (CONG_TAX == 1) ~ 1,
                         (CONG_CLI == 2) | (CONG_IMM == 2) | (CONG_TAX == 2) ~ 2,
                         (CONG_CLI == 3) | (CONG_IMM == 3) | (CONG_TAX == 3) ~ 3,
                         (CONG_CLI == 4) | (CONG_IMM == 4) | (CONG_TAX == 4) ~ 4))
longconjoint %>% count(CONG_ISSUE)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_ISSUE_B = case_when((CONG_CLI_B == 1) | (CONG_IMM_B == 1) | (CONG_TAX_B == 1) ~ 1,
                           (CONG_CLI_B == 0) | (CONG_IMM_B == 0) | (CONG_TAX_B == 0) ~ 0))

longconjoint %>% count(CONG_ISSUE_B)

#standardized
longconjoint$std_CONG_ISSUE <- (longconjoint$CONG_ISSUE + 4) / 8
longconjoint %>% count(std_CONG_ISSUE)

###Personality congruence
##narcisissm
#continious
longconjoint <- longconjoint %>%  mutate(
  CONG_NARC = case_when(
    (between(narcs, 4.50, 5.00) & Cpersonality == 1) | (between(narcs, 1.00, 1.50) & Cpersonality == 2) ~ 2,
    (between(narcs, 3.50, 4.50) & Cpersonality == 1) | (between(narcs, 1.50, 2.50) & Cpersonality == 2)~ 1,
    (between(narcs, 2.50, 3.50) & Cpersonality == 1) | (between(narcs, 2.50, 3.50) & Cpersonality == 2)~ 0,
    (between(narcs, 1.50, 2.50) & Cpersonality == 1) | (between(narcs, 3.50, 4.50) & Cpersonality == 2)~ -1,
    (between(narcs, 1.00, 1.50) & Cpersonality == 1) | (between(narcs, 4.50, 5.00) & Cpersonality == 2)~ -2,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_NARC)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_NARC_B = case_when(
    (between(narcs, 3.00, 5.00) & Cpersonality == 1) | (between(narcs, 1, 3.00) & Cpersonality == 2) ~ 1,
    (between(narcs, 1, 3.00) & Cpersonality == 1) | (between(narcs, 3.00, 5.00) & Cpersonality == 2)~ 0,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_NARC_B)

#standardized
longconjoint$std_CONG_NARC <- (longconjoint$CONG_NARC + 2) / 4
longconjoint %>% count(std_CONG_NARC)

##agreeableness
longconjoint <- longconjoint %>%  mutate(
  CONG_AGREE = case_when(
    (between(agree, 4.50, 5.00) & Cpersonality == 3) | (between(agree, 1.00, 1.50) & Cpersonality == 4) ~ 2,
    (between(agree, 3.50, 4.50) & Cpersonality == 3) | (between(agree, 1.50, 2.50) & Cpersonality == 4)~ 1,
    (between(agree, 2.50, 3.50) & Cpersonality == 3) | (between(agree, 2.50, 3.50) & Cpersonality == 4)~ 0,
    (between(agree, 1.50, 2.50) & Cpersonality == 3) | (between(agree, 3.50, 4.50) & Cpersonality == 4)~ -1,
    (between(agree, 1.00, 1.50) & Cpersonality == 3) | (between(agree, 4.50, 5.00) & Cpersonality == 4)~ -2,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_AGREE)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_AGREE_B = case_when(
    (between(agree, 3, 5) & Cpersonality == 3) | (between(agree, 1, 3) & Cpersonality == 4) ~ 1,
    (between(agree, 1, 3) & Cpersonality == 3) | (between(agree, 3, 5) & Cpersonality == 4)~ 0,
    TRUE ~ NA_real_))
longconjoint %>% count(CONG_AGREE_B)

#standardized
longconjoint$std_CONG_AGREE <- (longconjoint$CONG_AGREE + 2) / 4
longconjoint %>% count(std_CONG_AGREE)

##personality
longconjoint <- longconjoint %>%  mutate(
  CONG_PERSONALITY = case_when((CONG_NARC == -2) | (CONG_AGREE == -2) ~ -2,
                               (CONG_NARC == -1) | (CONG_AGREE == -1) ~ -1,
                               (CONG_NARC == 0) | (CONG_AGREE == 0) ~ 0,
                               (CONG_NARC == 1) | (CONG_AGREE == 1) ~ 1,
                               (CONG_NARC == 2) | (CONG_AGREE == 2) ~ 2))
longconjoint %>% count(CONG_PERSONALITY)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_PERSONALITY_B = case_when((CONG_NARC_B == 1) | (CONG_AGREE_B == 1) ~ 1,
                                 (CONG_NARC_B == 0) | (CONG_AGREE_B == 0) ~ 0))

longconjoint %>% count(CONG_PERSONALITY_B)

#standardized
longconjoint$std_CONG_PERSONALITY <- (longconjoint$CONG_PERSONALITY + 2) / 4
longconjoint %>% count(std_CONG_PERSONALITY)

##trait/ideology congruence
longconjoint <- longconjoint %>% 
  mutate(issue_ideology = case_when((Cissueposition == 1) | (Cissueposition == 3) | (Cissueposition == 5) ~ 'LIB',
                                    (Cissueposition == 2) | (Cissueposition == 4) | (Cissueposition == 6) ~ 'CON'))

longconjoint %>% count(issue_ideology) #making a new variable which signifies the ideological positioning of the claim

##openness
longconjoint <- longconjoint %>%  mutate(
  CONG_OPEN = case_when(
    (between(open, 4.50, 5.00) & issue_ideology == "LIB") | (between(open, 1.00, 1.50) & issue_ideology == "CON") ~ 2,
    (between(open, 3.50, 4.50) & issue_ideology == "LIB") | (between(open, 1.50, 2.50) & issue_ideology == "CON")~ 1,
    (between(open, 2.50, 3.50) & issue_ideology == "LIB") | (between(open, 2.50, 3.50) & issue_ideology == "CON") ~ 0,
    (between(open, 1.50, 2.50) & issue_ideology == "LIB") | (between(open, 3.50, 4.50) & issue_ideology == "CON")  ~ -1,
    (between(open, 1.00, 1.50) & issue_ideology == "LIB") | (between(open, 4.50, 5.00) & issue_ideology == "CON") ~ -2))
longconjoint %>% count(CONG_OPEN)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_OPEN_B = case_when(
    (between(open, 3.00, 5.00) & issue_ideology == "LIB") | (between(open, 1.00, 2.50) & issue_ideology == "CON")~ 1,
    (between(open, 1.00, 3.00) & issue_ideology == "LIB") | (between(open, 2.50, 5.00) & issue_ideology == "CON")~ 0))
longconjoint %>% count(CONG_OPEN_B)

#standardized
longconjoint$std_CONG_OPEN <- (longconjoint$CONG_OPEN + 2) / 4
longconjoint %>% count(std_CONG_OPEN)

##consiosness 
longconjoint <- longconjoint %>%  mutate(
  CONG_CON = case_when(
    (between(consc, 4.50, 5.00) & issue_ideology == "CON") | (between(consc, 1.00, 1.50) & issue_ideology == "LIB") ~ 2,
    (between(consc, 3.50, 4.50) & issue_ideology == "CON") | (between(consc, 1.50, 2.50) & issue_ideology == "LIB")~ 1,
    (between(consc, 2.50, 3.50) & issue_ideology == "CON") | (between(consc, 2.50, 3.50) & issue_ideology == "LIB") ~ 0,
    (between(consc, 1.50, 2.50) & issue_ideology == "CON") | (between(consc, 3.50, 4.50) & issue_ideology == "LIB")  ~ -1,
    (between(consc, 1.00, 1.50) & issue_ideology == "CON") | (between(consc, 4.50, 5.00) & issue_ideology == "LIB") ~ -2))
longconjoint %>% count(CONG_CON)

#binary
longconjoint <- longconjoint %>%  mutate(
  CONG_CON_B = case_when(
    (between(consc, 3.00, 5.00) & issue_ideology == "CON") | (between(consc, 1.00, 3.50) & issue_ideology == "LIB")~ 1,
    (between(consc, 1.00, 3.00) & issue_ideology == "CON") | (between(consc, 3.50, 5.00) & issue_ideology == "LIB")~ 0))

longconjoint %>% count(CONG_CON_B)

#standardized
longconjoint$std_CONG_CON <- (longconjoint$CONG_CON + 2) / 4
longconjoint %>% count(std_CONG_CON)

######DATA INSEPCTION#######
#did the conjoint work? 
longconjoint$gender_conjoint <- factor(longconjoint$Cgender, levels = rev(unique(longconjoint$Cgender)))
longconjoint$age_conjoint <- factor(longconjoint$Cage, levels = rev(unique(longconjoint$Cage)))
longconjoint$issueposition_conjoint <- factor(longconjoint$Cissueposition, levels = rev(unique(longconjoint$Cissueposition)))
longconjoint$personality_conjoint <- factor(longconjoint$Cpersonality, levels = rev(unique(longconjoint$Cpersonality)))

longconjoint$gender_conjoint <- recode_factor(longconjoint$gender_conjoint, "1" = "male", 
                                              "2" = "female")
longconjoint$age_conjoint <- recode_factor(longconjoint$age_conjoint, "1" = "GenZ", "2" = "Millenial", "3" = "GenX", "4" = "Boomer")
longconjoint$issueposition_conjoint  <- recode_factor(longconjoint$issueposition_conjoint, "1" = "proclimate", "2" = "anticlimate", "3" = "proimmigration", "4" = "antiimmigration", "5" = "protax", "6" = "antitax")
longconjoint$personality_conjoint <- recode_factor(longconjoint$personality_conjoint, "1" = "highnarcissism", "2" = "lownarcissism", "3" = "highagreeableness", "4" = "lowagreeableness")

f1 <- chosen ~ gender_conjoint + age_conjoint + issueposition_conjoint + personality_conjoint
plot(cj_freqs(longconjoint, f1, id = ~Respondent_Serial))

###distributions of model relevant variables#### 
longconjoint$country <- factor(longconjoint$country)
longconjoint$country <- recode_factor(longconjoint$country, "1" = "DE", "2" = "IT", "3" = "RO", "4" = "SW")
#agecongruence
distageC <- longconjoint %>% 
  ggplot(aes(CONG_AGE, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack") +
  labs(x = "age congruence", y = "Density")

##personality
distagreeC <- longconjoint %>% 
  ggplot(aes(CONG_AGREE, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack") +
  labs(x = "agreeableness congruence", y = "Density")

distnarcC <- longconjoint %>% 
  ggplot(aes(CONG_NARC, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack")+
  labs(x = "narcissism congruence", y = "Density")

distopenC <- longconjoint %>% 
  ggplot(aes(CONG_OPEN, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack") +
  labs(x = "openness congruence", y = "Density")

distconC <- longconjoint %>% 
  ggplot(aes(CONG_CON, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack") +
  labs(x = "conscientiousness congruence", y = "Density")

##issues
distimmC <- longconjoint %>% 
  ggplot(aes(CONG_IMM, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack") +
  labs(x = "immigration congruence", y = "Density")

distcliC <- longconjoint %>% 
  ggplot(aes(CONG_CLI, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack") +
  labs(x = "climate congruence", y = "Density")

disttaxC <- longconjoint %>% 
  ggplot(aes(CONG_TAX, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack")+
  labs(x = "taxation congruence", y = "Density")

#feeling represented
distFR <- longconjoint %>% 
  ggplot(aes(Crating, fill = country)) +
  geom_density(alpha = 0.5, bw = 0.75, position = "stack") +
  labs(x = "feeling represented", y = "Density")

#full plot congruences; model variables
ggarrange(distageC, distagreeC, distnarcC, distopenC, distconC, distimmC, distcliC, disttaxC, distFR, font.label = list(size = 10, face = "plain", color="black"), 
          align = "hv", legend = "right", common.legend = TRUE) 
#warning is just for the missing values that are present for the congruences.

write.csv(longconjoint, file = "longconjointdata.csv")










